<!doctype html>
<html>
<head>
	<title>Upload</title>
</head>
<body>
	<?php 
            /*
             * Verify if the submit button has been pushed
             */
            if(isset($_POST["submit"]) && !empty($_POST["submit"]))
            {
                /*
                 * Save the allowed extension of the upload file in an array
                 * Save the extension of the uploaded file
                 */
                $allowedExtensions = array("jpeg"  , "jpg" , "png");
                $extension = end(explode("." , $_FILES["file"]["name"]));
                /*
                 * Verify if the type of the uploaded file matches the allowed types
                 * and the size of the file is less than 20kb
                 * and the extension exists in the array containing the allowed extensions list
                 */
                if(($_FILES["file"]["type"] == "image/png"
                        || $_FILES["file"]["type"] == "image/jpg"
                        || $_FILES["file"]["type"] == "image/jpeg")
                        && $_FILES["file"]["size"] < 20000
                        && in_array($extension , $allowedExtensions))
                {
                    /*
                     * If there are errors when uploading the file, display them
                     * Else, display the information about the uploaded file(name, type etc.)
                     */
                    if($_FILES["file"]["error"]>0)
                    {
                            echo "Error: ".$_FILES["file"]["error"];
                    }
                    else
                    {
                            echo "File name: " . $_FILES["file"]["name"] . "<br/>";
                            echo "File type: " . $_FILES["file"]["type"] . "<br/>";
                            echo "File size: " . ($_FILES["file"]["size"]/1024) . "kb" . "<br/>";
                            echo "File location: "  .$_FILES["file"]["tmp_name"]."<br/>";
                    }
                    /*
                     * If the file was already uploaded, display an "File already uploaded" message
                     * Else, move the file from the temporary directory into the chosen directory
                     */
                    if(file_exists($_FILES["file"]["name"]))
                    {
                            echo "File already uploaded";
                    }
                    else
                    {
                            move_uploaded_file($_FILES["file"]["tmp_name"] , $_FILES["file"]["name"]);
                            echo "Stored in: " . $_FILES["file"]["name"];
                    }
                }
                else
                {
                    echo "Invalid file";
                }
            }
            else
            {
	?>
	<form action="" method="post" enctype="multipart/form-data">
		<label for="file">Filename:</label>
		<input type="file" name="file" id="file" /><br/>
		<input type="submit" name="submit" value="Submit">  
	</form>
        <?php
            }
        ?>
</body>
</html>